package com.shenmazong.ware.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.shenmazong.utils.IdWorker;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.MessageVo;
import com.shenmazong.ware.service.MessageService;
import com.shenmazong.ware.service.impl.IMessageService1Impl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2022/7/5 11:35
 */

@RestController
@Slf4j
@RequestMapping(value = "/msg")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private IMessageService1Impl iMessageService1;

    @PostMapping(value = "/msg")
    public ResultResponse test(@RequestParam("msg") String msg) {
        MessageVo messageVo = new MessageVo();
        messageVo.setId(IdWorker.getId());
        messageVo.setType(1);
        messageVo.setData(msg);

//        return messageService.sendMessage(messageVo);
        return iMessageService1.sendMessage(messageVo);
    }


    @PostMapping(value = "/msg1")
    @HystrixCommand(
            fallbackMethod = "handleError",
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
            })
    public ResultResponse test1(@RequestParam("msg") String msg) {
        MessageVo messageVo = new MessageVo();
        messageVo.setId(IdWorker.getId());
        messageVo.setType(1);
        messageVo.setData(msg);

        return iMessageService1.sendMessage(messageVo);
    }

    public ResultResponse handleError(@RequestParam("msg") String msg) {
        System.out.println("handleError(controller):熔断了");
        return ResultResponse.FAILED(404);
    }
}
